package com.schoolinfo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sina.sae.storage.SaeStorage;

public class FileUtil {
	private static String localTmpDir=System.getProperty("java.io.tmpdir");
	
	private static String domainName ="schoolinfo";
	
	public static String getLocalDir()
	{
		return localTmpDir;//+domainName+"\\";
	}
	
	public static boolean write(String fileName,String content)
	{
		SaeStorage storage = new SaeStorage();
		return storage.write(domainName, fileName, content);		
	}
	
	public static boolean write(String fileName,String floderName,byte[] bytes)
	{
		SaeStorage storage = new SaeStorage();
		return storage.write(domainName+"\\"+floderName, fileName, bytes);
	}
	
	public static boolean write(String fileName,byte[] bytes)
	{
		SaeStorage storage = new SaeStorage();
		return storage.write(domainName, fileName, bytes);
	}
	
	public static byte[] read(String fileName)
	{
		SaeStorage storage = new SaeStorage();
		return storage.read(domainName, fileName);
	}
	
	public static String getUrl(String fileName)
	{
		SaeStorage storage = new SaeStorage();
		return storage.getUrl(domainName, fileName);
	} 
	
	public static void main(String args[])
	{
		String path ="F:\\图片\\禁止.jpg";
		File file = new File(path);
		try {
			FileInputStream fis = new FileInputStream(file);
			//File newFile=new File(FileUtil.getLocalDir()+"SaeStoageDir\\null\\schoolinfo\\pic\\a.jpg");
		//	FileOutputStream out = new FileOutputStream(newFile);
			//byte[] bytes=new byte[1024];
			byte[] fileBytes= new byte[(int) file.length()];
			fis.read(fileBytes);
			FileUtil.write("b.jpg","pic",fileBytes);
			System.out.println("文件大小："+file.length()+"\n"+FileUtil.getUrl("pic/b.jpg"));
			
			/*
			int count=0;
			
			while((count=fis.read(bytes))>-1)
			{
				
				out.write(bytes, 0, count);
			}
			
			out.flush();
			out.close();
			*/
			fis.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	//	System.out.println(FileUtil.getLocalDir());
		//FileUtil.write("a.txt", "hellon\nr\rnokrn\r\nonr\n\rend");
		
	}
	
	
	
}
